#include <iostream>
#include <mutex>
#include <pthread.h>

//自己封装系统锁的接口
class Mutex
{
public:
    Mutex()
    {
        //系统初始化锁
        pthread_mutex_init(&_lock,nullptr);
    }
    //创建锁
    void Lock()
    {
        pthread_mutex_lock(&_lock);
    }
    //解锁
    void Unlock()
    {
        pthread_mutex_unlock(&_lock);
    }
    ~Mutex()
    {
        //系统删除锁
        pthread_mutex_destroy(&_lock);
    }
private:
    pthread_mutex_t _lock;
};

//进行二次封装实现对象生命周期结束时自动释放，避免资源泄漏。
//RAII
class LockGuard
{
public:
    LockGuard(Mutex*_mutex):_mutexp(_mutex)
    {
        _mutex->Lock();

    }
    ~LockGuard()
    {
        _mutexp->Unlock();
    }
private:
    //成员变量是个锁
    Mutex* _mutexp;
};